package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report")
public class ReportController {

    @Reference
    private SetmealService setmealService;
    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    @Reference
    private ReportService reportService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport(@RequestBody Map maps){

        try {
            Map map = memberService.getMemberReport(maps);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

    /**
     * 获取预约套餐数据
     */
    @RequestMapping("getSetmealReport")
    public Result getSetmealReport(){

        try {
            Map map = orderService.getSetmealReport();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    /**
     * 获取运营数据
     */
    @RequestMapping("getBusinessReport")
    public Result getBusinessReport(){

        try {
            Map map = reportService.getBusinessReport();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    @RequestMapping("exportBusinessReport")
    public void exportBusinessReport(HttpSession session, HttpServletResponse response){

        try {
            String realPath = session.getServletContext().getRealPath("/template/report_template.xlsx");

            XSSFWorkbook workbook = new XSSFWorkbook(realPath);

            ServletOutputStream outputStream = response.getOutputStream();

            Map map = reportService.getBusinessReport();
            /*
                    reportDate:null,//报告日期
                    todayNewMember :0,//今日新增会员数
                    totalMember :0,//总会员数
                    thisWeekNewMember :0,//本周新增会员数
                    thisMonthNewMember :0,//本月新增会员数
                    todayOrderNumber :0,//今日预约数
                    todayVisitsNumber :0,//今日到诊数
                    thisWeekOrderNumber :0,//本周预约数
                    thisWeekVisitsNumber :0,//本周到诊数
                    thisMonthOrderNumber :0,//本月预约数
                    thisMonthVisitsNumber :0,//本月到诊数
                    hotSetmeal :[//显示4个热门套餐
                    */
            String reportDate = (String) map.get("reportDate");

            Integer todayNewMember = (Integer) map.get("todayNewMember");
            Integer totalMember = (Integer) map.get("totalMember");
            Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");
            List<Map> list = (List) map.get("hotSetmeal");

            //获取工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取第二行
            XSSFRow row = sheet.getRow(2);
            //获取第五个单元格
            row.getCell(5).setCellValue(reportDate);

            row = sheet.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            row.getCell(7).setCellValue(totalMember);

            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            row.getCell(7).setCellValue(thisMonthNewMember);

            row = sheet.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            row.getCell(7).setCellValue(todayVisitsNumber);

            row = sheet.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            row.getCell(7).setCellValue(thisWeekVisitsNumber);

            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            row.getCell(7).setCellValue(thisMonthVisitsNumber);


            int rowNum = 12;
            for (Map m : list) {

                String name = (String) m.get("name");
                Long setmeal_count = (Long) m.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) m.get("proportion");

                row = sheet.getRow(rowNum);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);
                row.getCell(6).setCellValue(proportion.toString());

                rowNum++;
            }

            //设置响应的文件格式
            response.setContentType("application/vnd.ms-excel");
            //设置下载方式
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");

            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    /*
     *   第二个饼图
     */
    @RequestMapping("/getMemberReportBySex")
    public Result getMemberReportBySex(){
        System.out.println("asfasdfdasf000000");
        try{
            Map map=setmealService.getMemberReportBySex();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    /*
     * 第三个饼图
     */
    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport(){
        try {
            List<Map<String,Object>> list = memberService.findMemberAgeCount();
            Map<String,Object> map = new HashMap<>();
            map.put("ageCount",list);
            List<String> memberAge = new ArrayList<>();
            for (Map<String, Object> maplist : list) {
                String name = (String) maplist.get("name");
                memberAge.add(name);
            }
            map.put("memberAge",memberAge);
            return new Result(true,MessageConstant.GET_MEMBERAGE_COUNT_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBERAGE_COUNT_REPORT_FAIL);
        }
    }
}
